package com.myacg.editor.common.unit.panel.mobilepanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import com.myacg.editor.common.unit.button.ActivButton;
/**
 * 1.存放移动面板的容器
 * 2.掌管移动面板的生命周期以及UI展示
 * @author Administrator
 *
 */
public class MobilePanelManager extends JPanel{
	private Map<String , MobilePanel> panels;
	private Map<String , ActivButton>  buttons;
	private JLayeredPane  centerPanel; //展示面板的栏
	private JPanel  statePanel;  //下方的状态栏
	public MobilePanelManager() {
		panels = new HashMap<String, MobilePanel>();
		buttons =  new HashMap<String, ActivButton>();
		centerPanel = new JLayeredPane(); //设置为绝对布局
		//设置面板背景透明
		centerPanel.setOpaque(false);
		this.setOpaque(false);
		//加入状态栏
		statePanel  = new JPanel(new FlowLayout(0,1,0)){
			Color bgColor = new Color(240 , 240, 240 );
			Color borderColor = new Color(210 , 210, 210 );
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				g.setColor(bgColor);
				g.fillRect(0, 0, getWidth(), getHeight());
				g.setColor(borderColor);
				g.drawLine(0, getHeight()-1, getWidth(), getHeight()-1);
			}
		};
		statePanel.setPreferredSize(new Dimension(statePanel.getWidth(), 30));
		
		this.setLayout(new BorderLayout());
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(statePanel, BorderLayout.NORTH);
	}
	
	public MobilePanel getMobilePanel(String panelId){
		return this.panels.get(panelId);
	}
	public ActivButton getActivButton(String buttonId){
		return this.buttons.get(buttonId);
	}
	public void removeMobilePanel(String panelId){
		this.centerPanel.remove(getMobilePanel(panelId));
		this.panels.remove(panelId);
		//并且移除对应的按钮
		this.statePanel.remove(getActivButton(panelId));
		this.buttons.remove(panelId);
	}
	
	
	/**
	 * 加入一个移动面板到管理器中
	 * @param mobilePanel
	 */
	public synchronized MobilePanel  addMobelPanel(String panelName,Rectangle bounds){
		//加入面板
		MobilePanel  mobilePanel = new MobilePanel(panelName) {
			@Override
			public void hideEvent() {
				buttons.get(this.getId()).unActivated();
			}
			@Override
			public void closeEvent() {
				//通知管理器移除自己
				removeMobilePanel(this.getId());
				System.out.println(this.getId() + " --已移除");
				MobilePanelManager.this.updateUI();
			}
		};
		//加入面板点击前置事件
		mobilePanel.getPanelBar().addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				//移动组件到顶端
				centerPanel.moveToFront(e.getComponent().getParent());
			}
		});
		this.panels.put(mobilePanel.getId(), mobilePanel);
		this.centerPanel.add(mobilePanel);
		mobilePanel.setBounds(bounds); //设置位置
		//增加一个状态按钮
		ActivButton  activButton= new ActivButton(panelName ,mobilePanel.getId()) {
			@Override
			public void unActivatedEvent() {
				getMobilePanel(this.getButtonId()).setVisible(false);
			}
			@Override
			public void activatedEvent() {
				MobilePanel panel = getMobilePanel(this.getButtonId());
				centerPanel.moveToFront(panel);
				panel.setVisible(true);
			}
		};
		this.statePanel.add(activButton);
		activButton.setPreferredSize(new Dimension(100, 28));
		this.buttons.put(activButton.getButtonId(), activButton);
		activButton.unActivated();//触发非激活
		//刷新组件
		this.updateUI();
		//最终反馈这个面板对应的panelId
		return mobilePanel;
	}
}
